package client;

import base.BaseValue;
import client.connect.HttpConnect;
import client.control.Controller;
import client.control.ResponseEntity;
import client.entity.User;
import com.alibaba.fastjson.JSONObject;

import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.IOException;
import java.util.List;
import java.util.Properties;

/**
  * @Description 存有一些程序运行过程中需要的全局变量
  * @author 孔明浩
  * @Date 2020/4/1
  */
public class UserState {
    //是否登录
    private static Boolean login = false;
    //是否记住密码
    private static Boolean remember = false;
    //token
    private static String token = "";
    //用户id
    private static Integer id ;
    //用户名
    private static String username = "";
    //密码
    private static String password = "";
    //用户数据
    private static User user = new User();
    public static void initData(){
        FileInputStream fis = null;
        try {
            File file = initFile("data.properties");
            fis = new FileInputStream(file);
            Properties properties = new Properties();
            properties.load(fis);

            if (properties.getProperty("login") != null){
                UserState.login = Boolean.parseBoolean(properties.getProperty("login"));
            }
            if (properties.getProperty("token") != null){
                UserState.token = properties.getProperty("token");
                new Thread(new Runnable() {
                    @Override
                    public void run() {
                        ResponseEntity responseEntity = Controller.USER_CONTROL.getUserInfo(token);
                        if (responseEntity.getCode() == 0){
                            user = JSONObject.parseObject(responseEntity.getData(), User.class);
                            username = user.getUserName();
                            id = user.getUserId();
                        }
                    }
                }).start();
            }
            if (properties.getProperty("username") != null){
                UserState.username = properties.getProperty("username");
            }
            if (properties.getProperty("password") != null){
                UserState.password = properties.getProperty("password");
            }
            if (properties.getProperty("userId") != null){
                UserState.id = Integer.parseInt(properties.getProperty("userId"));
            }
        } catch (IOException e){
            e.printStackTrace();
        } finally {
            try {
                if (fis != null){
                    fis.close();
                }
            } catch (IOException e){
                e.printStackTrace();
            }
        }
    }
    public static boolean update(Boolean login, String token, String username,String password, Integer userId){
        File file = null;
        FileInputStream fis = null;
        FileOutputStream fos = null;
        try {
            file = initFile("data.properties");
            fos = new FileOutputStream(file);
            fis = new FileInputStream(file);
            Properties properties = new Properties();
            properties.load(fis);
            if (login != null){
                UserState.login = login;
                properties.setProperty("login",login.toString());
            }
            if (token != null){
                UserState.token = token;
                properties.setProperty("token",token);
            }
            if (username != null){
                UserState.username = username;
                properties.setProperty("username",username);
            }
            if (password != null){
                UserState.password = password;
                properties.setProperty("password",password);
            }
            if (userId != null){
                UserState.id = userId;
                properties.setProperty("userId", id + "");
            }
            properties.store(fos,null);
        } catch (IOException e){
            e.printStackTrace();
            return false;
        } finally {
            try {
                if (fis != null){
                    fis.close();
                }
                if (fos != null){
                    fos.close();
                }
            } catch (IOException e){
                e.printStackTrace();
            }
        }
        return true;
    }

    public static void logout(){
        File file = initFile("data.properties");
        login = false;
        token = "";
        username = "";
        id = null;
        new Thread(new Runnable() {
            @Override
            public void run() {
                Controller.USER_CONTROL.logout(token);
            }
        }).start();
        file.delete();
    }

    private static File initFile(String url){
        File file = new File(url);
        if (!file.exists()){
            try{
                file.createNewFile();
            }catch (IOException e){
                e.printStackTrace();
            }
        }
        return file;
    }

    public static Boolean isLogin() {
        return login;
    }

    public static void setLogin(Boolean login) {
        UserState.login = login;
    }

    public static void setRemember(Boolean remember) {
        UserState.remember = remember;
    }

    public static String getToken() {
        return token;
    }

    public static void setToken(String token) {
        UserState.token = token;
    }

    public static Integer getId() {
        return id;
    }

    public static void setId(Integer id) {
        UserState.id = id;
    }

    public static String getUsername() {
        return username;
    }

    public static void setUsername(String username) {
        UserState.username = username;
    }

    public static String getPassword() {
        return password;
    }

    public static void setPassword(String password) {
        UserState.password = password;
    }

    public static User getUser() {
        return user;
    }

    public static void setUser(User user) {
        if (user == null){
            logout();
            return;
        }
        UserState.username = user.getUserName();
        UserState.id = user.getUserId();
        UserState.user = user;
        if (remember){
            update(UserState.isLogin(), token, user.getUserName(), "", user.getUserId());
        }
    }
}
